import 'dart:convert';
import 'package:flutter/services.dart';

import 'package:blind_traveler/modules/home/tabs/tabs.dart';
import 'package:get/get.dart';

import 'home_state.dart';
import '../../../models/models.dart';

class HomeLogic extends GetxController {
  final state = HomeState();

  RxList userMessages = [].obs;

  @override
  void onInit() {
    super.onInit();
    state;
    readMessages();
  }

  void changeOnLine(value) => state.onLine.value = value;

  void readMessages() {
    rootBundle.loadString('assets/messages.json').then((value) {
      final data = json.decode(value);
      for (var i = 0; i < data.length; i++) {
        UserMessages userMessage = UserMessages.fromJson(data[i]);
        userMessage.toJson();
        userMessages.add(userMessage);
      }
    });
  }

  void switchTab(index) {
    var tab = _getCurrentTab(index);
    state.currentTab.value = tab;
  }

  int getCurrentIndex(MainTabs tab) {
    switch (tab) {
      case MainTabs.messages:
        return 0;
      case MainTabs.matching:
        return 1;
      case MainTabs.me:
        return 2;
      default:
        return 0;
    }
  }

  MainTabs _getCurrentTab(int index) {
    switch (index) {
      case 0:
        return MainTabs.messages;
      case 1:
        return MainTabs.matching;
      case 2:
        return MainTabs.me;
      default:
        return MainTabs.messages;
    }
  }
}
